package com.kcl.dfss;

import java.io.File;

import com.kcl.dfss.http.CameraConnection;
import com.kcl.dfss.utils.FTPUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MatchingCameraActivity extends Activity {
	
	private LinearLayout matching_camera_back;
	private Button btn_cameranext;
	private String md5 = null;
	
	boolean cameraisconnect  = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_matching_camera);
		
		matching_camera_back = (LinearLayout) findViewById(R.id.matching_camera_back);
		btn_cameranext = (Button) findViewById(R.id.btn_cameranext);
		
		cameraisconnect = LocationService.getCamemraConnected(); 
		
		matching_camera_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		btn_cameranext.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				FTPUtils.saveJsonTosdCard(MatchingCameraActivity.this);
				md5 = FTPUtils.getFileMD5(new File(Environment.getExternalStorageDirectory()+"/camcfg.json"));
				if(cameraisconnect){
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							FTPUtils.ftpUpload("192.168.1.1", "21", "root", "", Environment.getExternalStorageDirectory().toString(),
									"/camcfg.json");
							
							final String result = CameraConnection.saveFile("camcfg.json", md5);
							runOnUiThread(new Runnable() {
								
								@Override
								public void run() {
									Toast.makeText(MatchingCameraActivity.this, result, Toast.LENGTH_SHORT).show();
								}
							});
						}
					}).start();
					
					Intent intent = new Intent();
//					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.setClass(MatchingCameraActivity.this,MainActivity.class);
					startActivity(intent);
					finish();
				}else{
					Intent intent = new Intent();
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.setClass(MatchingCameraActivity.this,MainActivity.class);
					startActivity(intent);
					finish();
				}
				
			}
		});
	}

}
